home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 08 - 1992 / 08.04 Aug 92 / Hello TCL World / HelloApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  5.7 KB  |  254 lines  |  [TEXT/KAHL]

  1. /*
  2.  * HelloApp.c
  3.  *  Copyright © 1991 Martin Minow. All rights reserved.
  4.  * All use and non-commercial distribution permitted.
  5.  */
  6. #include <CBartender.h>
  7. #include <CBureaucrat.h>
  8. #include <CDesktop.h>
  9. #include <CFWDesktop.h>
  10. #include <Commands.h>
  11. #include <TBUtilities.h>
  12. #include "HelloApp.h"
  13.  
  14. extern CBartender    *gBartender;
  15. extern CDesktop        *gDesktop;
  16. extern CBureaucrat    *gGopher;
  17.  
  18. /*
  19.  * IHelloApp -- the only thing this does is display the
  20.  * logging window on the screen. It reacts to commands
  21.  * by displaying the command number and sending them
  22.  * onwards to the TCL CApplication method.
  23.  */
  24. void
  25. HelloApp::IHelloApp(void)
  26. {
  27.         CApplication::IApplication(8, 32768L, 4096L);
  28.         itsTextWindow = NULL;
  29.         MakeTextWindow(CmdMakeNoFloatHide);    /* Normal window    */
  30.         itsTextWindow->SetTitle(
  31.             (StringPtr) "\pHello (TCL) World!"
  32.         );
  33.         itsTextWindow->SetText(
  34.             (StringPtr) "\pHello world!"
  35.         );
  36.         gGopher = this;
  37. }
  38.  
  39. /*
  40.  * Override MakeDesktop to allow creation of floating windows.
  41.  */
  42. void
  43. HelloApp::MakeDesktop()
  44. {
  45.         gDesktop = new(CFWDesktop);
  46.         ((CFWDesktop *) gDesktop)->IFWDesktop(this);
  47. }
  48.  
  49. /*
  50.  * SetupMenus is called (by CApplication::IApplication).
  51.  * It adds our application-specific menus to the menu bar.
  52.  */
  53. void
  54. HelloApp::SetUpMenus()
  55. {
  56.         Str255                work;
  57.         short                MENUid;
  58.         short                itemNo;
  59.         MenuHandle            macMenu;
  60.         
  61.         inherited::SetUpMenus();
  62.         /*
  63.          * Add some menu items to test UnknownCommand
  64.          */
  65.         gBartender->SetCmdText(
  66.             cmdAbout,
  67.             (StringPtr) "\pAbout Hello World"
  68.         );
  69.         gBartender->SetDimOption(MENU_Test, dimNONE);
  70.         gBartender->SetDimOption(MENU_MakeTextWindow, dimNONE);
  71.         AddResMenu(GetMHandle(MENUfont), 'FONT');
  72.         gBartender->SetDimOption(MENUfont, dimNONE);
  73.         gBartender->SetUnchecking(MENUfont, TRUE);
  74.         /*
  75.          * Build the TextWindow menu command on the fly.
  76.          * You can also just add the command to a menu
  77.          * in your resource file.
  78.          */
  79.         GetIndString(work, STRS_Toggle, kShowAuxWindow);
  80.         gBartender->FindMenuItem(
  81.             cmdToggleClip,
  82.             &MENUid,
  83.             &macMenu,
  84.             &itemNo
  85.         );
  86.         if (macMenu != NULL && work[0] != 0) {
  87.             gBartender->InsertMenuCmd(
  88.                 CmdToggleTextWindow,
  89.                 work,
  90.                 MENUid,
  91.                 itemNo
  92.             );
  93.         }
  94. }
  95.  
  96. /*
  97.  * UpdateMenus is called before showing a menu. Make sure
  98.  * the Show/Hide text box command is enabled.
  99.  */
  100. void
  101. HelloApp::UpdateMenus()
  102. {
  103.         inherited::UpdateMenus();
  104.         if (itsTextWindow != NULL)
  105.             gBartender->EnableCmd(CmdToggleTextWindow);
  106. }
  107.  
  108. /*
  109.  * Execute a command.  Only CmdToggleTextWindow does anything
  110.  * useful.
  111.  */    
  112. void
  113. HelloApp::DoCommand(
  114.         long        theCommand
  115.     )
  116. {
  117.         switch (theCommand) {
  118.         case CmdMakeNoFloatHide:
  119.         case CmdMakeNoFloatShow:
  120.         case CmdMakeIsFloatHide:
  121.         case CmdMakeIsFloatShow:
  122.             MakeTextWindow(theCommand);    break;
  123.         case CmdToggleTextWindow:
  124.             itsTextWindow->Toggle();    break;
  125.         case cmdAbout:
  126.             if (itsTextWindow == NULL)
  127.                 MakeTextWindow(CmdMakeNoFloatHide);
  128.             itsTextWindow->SetTitle(
  129.                 (StringPtr) "\pHello (TCL) World!"
  130.             );
  131.             itsTextWindow->SetText(
  132.                 (StringPtr) "\pHello world!"
  133.             );
  134.             itsTextWindow->SetText(
  135.                 (StringPtr) "\pCopyright © 1991"
  136.                     " Martin Minow,"
  137.                     " All rights reserved."
  138.             );
  139.             break; 
  140.         case cmdNew:
  141.             if (itsTextWindow != NULL)
  142.                 itsTextWindow->SetText(
  143.                     (StringPtr) "\pNew selected");
  144.             break;
  145.         case cmdOpen:
  146.             if (itsTextWindow != NULL)
  147.                 itsTextWindow->SetText(
  148.                     (StringPtr) "\pOpen selected");
  149.             break;
  150.         default:
  151.             /*
  152.              * I've found it useful to log commands
  153.              * that aren't executed by my application
  154.              * This catchs errors in setting the gopher
  155.              * in a complicated web of sub-classes.
  156.              */
  157.             UnknownCommand(theCommand);
  158.             inherited::DoCommand(theCommand);
  159.         }
  160. }
  161.  
  162. void
  163. HelloApp::MakeTextWindow(
  164.         long            theCommand
  165.     )
  166. {
  167.         Boolean            isFloating;
  168.         Boolean            hideOnSuspend;
  169.         Str255            titleText;
  170.         
  171.         if (itsTextWindow != NULL)
  172.             itsTextWindow->Dispose();
  173.         isFloating = (
  174.                 theCommand == CmdMakeIsFloatHide
  175.              ||    theCommand == CmdMakeIsFloatShow
  176.             );
  177.         hideOnSuspend = (
  178.                 theCommand == CmdMakeNoFloatHide
  179.              ||    theCommand == CmdMakeIsFloatHide
  180.             );
  181.         itsTextWindow = new(TextWindow);
  182.         itsTextWindow->ITextWindow(
  183.             this,                    /* The application    */
  184.             WIND_Note,                /* WIND Resource id    */
  185.             isFloating,                /* Not floating        */
  186.             hideOnSuspend,            /* Don't hide        */
  187.             CmdToggleTextWindow,    /* Show/Hide cmd    */
  188.             STRS_Toggle,            /* Show/Hide STR#    */
  189.             (StringPtr) "\pgeneva",    /* Font name         */
  190.             9                        /* Font size value    */
  191.         );
  192.         gBartender->GetCmdText(theCommand, titleText);
  193.         itsTextWindow->SetTitle(titleText);
  194.         itsTextWindow->Show();
  195. }
  196.  
  197.  
  198. /*
  199.  * Unrecognized commands (from the application) come here.
  200.  * This method does nothing for TCL commands (cmdQuit,
  201.  * cmdNull) that are normally handled by the TCL
  202.  * CApplication method, and logs the command or menu
  203.  * number for those the application should have handled.
  204.  * If commands are logged, they may indicate menu items
  205.  * are enabled incorrectly, the Gopher set incorrectly,
  206.  * or a class failing to handle commands directed at it.
  207.  */
  208. void
  209. HelloApp::UnknownCommand(
  210.         long            theCommand
  211.     )
  212. {
  213.         short            theMenu;
  214.         short            theItem;
  215.         Str255            work;
  216.         Str255            number;
  217.         
  218.         theMenu = HiShort(-theCommand);
  219.         theItem = LoShort(-theCommand);
  220.         if (theCommand < 0 && theMenu == MENUapple)
  221.             ;                /* These are aways ok        */
  222.         else {
  223.             switch (theCommand) {
  224.             case cmdNull:
  225.             case cmdQuit:
  226.             case cmdToggleClip:
  227.                 break;
  228.             default:
  229.                 if (theCommand >= 0) {
  230.                     CopyPString(
  231.                         (StringPtr) "\pUnknown command ",
  232.                         work
  233.                     );
  234.                     NumToString(theCommand, number);
  235.                 }
  236.                 else {
  237.                     CopyPString(
  238.                         (StringPtr) "\pUnknown menu ",
  239.                         work
  240.                     );
  241.                     NumToString(theMenu, number);
  242.                     ConcatPStrings(work, number);
  243.                     ConcatPStrings(work, (StringPtr) "\p.");
  244.                     NumToString(theItem, number);
  245.                 }
  246.                 ConcatPStrings(work, number);
  247.                 if (itsTextWindow != NULL)
  248.                     itsTextWindow->SetText(work);
  249.             }
  250.         }
  251. }
  252.  
  253.  
  254.